using System;
using System.Collections.Generic;
using System.Text.RegularExpressions;

namespace UserAuthApp
{
    public class UserService : IUserService
    {
        // d. 泛型集合保存注册用户
        private readonly List<User> _users = new List<User>();

        // --------------- 流程总控 ---------------
        public void Run()
        {
            while (true)
            {
                Console.WriteLine("\n===== 用户认证系统 =====");
                Console.WriteLine("1. 登录");
                Console.WriteLine("2. 注册");
                Console.WriteLine("3. 退出");
                Console.Write("请选择操作：");

                switch (Console.ReadLine())
                {
                    case "1":
                        Login();
                        break;
                    case "2":
                        Register();
                        break;
                    case "3":
                        Console.WriteLine("感谢使用，再见！");
                        return;
                    default:
                        Console.WriteLine("错误：请输入有效数字(1-3)");
                        break;
                }
            }
        }

        // --------------- 注册 ---------------
        public void Register()
        {
            Console.WriteLine("\n--- 用户注册 ---");

            string userName = ReadUserName();
            string password = ReadPassword();

            _users.Add(new User(userName, password));
            Console.WriteLine("注册成功！");
        }

        // --------------- 登录 ---------------
        public void Login()
        {
            Console.WriteLine("\n--- 用户登录 ---");

            Console.Write("请输入用户名：");
            string name = Console.ReadLine().Trim();

            // e. foreach 迭代查询
            User cur = null;
            foreach (var u in _users)
            {
                if (u.UserName == name)
                {
                    cur = u;
                    break;
                }
            }

            if (cur == null)
            {
                Console.WriteLine("错误：用户名不存在");
                return;
            }

            Console.Write("请输入密码：");
            string pwd = Console.ReadLine();

            if (cur.Password != pwd)
            {
                Console.WriteLine("错误：密码不正确");
                return;
            }

            Console.WriteLine("登录成功！欢迎进入系统");
            Console.WriteLine("模拟系统首页内容...");
        }

        // --------------- 私有辅助 ---------------
        private string ReadUserName()
        {
            while (true)
            {
                Console.Write("请输入用户名(3-15字符)：");
                string name = Console.ReadLine().Trim();

                if (string.IsNullOrEmpty(name))
                {
                    Console.WriteLine("错误：用户名不可为空");
                    continue;
                }
                if (!ValidateUserName(name))
                {
                    Console.WriteLine("错误：用户名必须为3-15字符，仅包含字母、数字、下划线");
                    continue;
                }

                // 重名校验
                bool exists = false;
                foreach (var u in _users)   // e. foreach 迭代
                {
                    if (u.UserName == name)
                    {
                        exists = true;
                        break;
                    }
                }
                if (exists)
                {
                    Console.WriteLine("错误：用户名已存在");
                    continue;
                }

                return name;
            }
        }

        private string ReadPassword()
        {
            while (true)
            {
                Console.Write("请输入密码(6-20字符，必须包含大小写字母和数字)：");
                string pwd = Console.ReadLine();

                if (string.IsNullOrEmpty(pwd))
                {
                    Console.WriteLine("错误：密码不可为空");
                    continue;
                }
                if (!ValidatePassword(pwd))
                {
                    Console.WriteLine("错误：密码必须包含大小写字母和数字，长度6-20字符");
                    continue;
                }

                Console.Write("请再次输入密码：");
                string again = Console.ReadLine();

                if (pwd != again)
                {
                    Console.WriteLine("错误：两次输入的密码不一致");
                    continue;
                }

                return pwd;
            }
        }

        private static bool ValidateUserName(string name)
            => name.Length is >= 3 and <= 15 && Regex.IsMatch(name, @"^[a-zA-Z0-9_]+$");

        private static bool ValidatePassword(string pwd)
        {
            if (pwd.Length < 6 || pwd.Length > 20) return false;
            if (!Regex.IsMatch(pwd, @"[A-Z]")) return false;
            if (!Regex.IsMatch(pwd, @"[a-z]")) return false;
            return Regex.IsMatch(pwd, @"\d");
        }
    }
}